#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@NAME          : genttfhtml.py
@TIME          : 2024/11/21 18:55:16
@AUTHOR        : chenlip
@VERSION       : 0.0.1
@DESCRIPTION   : 小红书爬虫的初始化
'''
# --------------------------引用--------------------------
import json
import os

from fake_useragent import UserAgent
from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.edge.service import Service as EdgeService

from Config import RedBookConf as Config

class RedInit(object):

    def __init__(self) -> None:
        pass

    def init_browser():
        options = Options()
        options.add_argument("--disable-blink-features=AutomationControlled")
        options.add_argument('--disable-gpu')
        options.add_argument('--disable-dev-shm-usage')
        options.add_argument('--no-sandbox')
        options.add_argument(f'user-agent=={UserAgent().random}')
        # 无界面模式
        options.add_argument('--headless')

        # 忽略特定的错误信息
        options.add_argument('--log-level=3')
        options.add_argument('--disable-extensions')
        options.add_argument('--disable-infobars')
        options.add_argument('--disable-notifications')

        # webdriver_path = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe"
        # edge_service = EdgeService(executable_path=webdriver_path)
        options.add_experimental_option('debuggerAddress', '127.0.0.1:18088')
        Config.Browser = webdriver.Edge(options=options)
        Config.Browser.maximize_window()


    def init_cookie():
        """读取本地 Cookie"""
        if os.path.isfile("cookies.json"):
            with open("cookies.json", "r+", encoding="utf-8") as f:
                content = f.read()
                if content:
                    Config.CookiesDict.update(json.loads(content))
                else:
                    print("Cookie 文件为空！")
                    Config.login_status = True
                    return
        else:
            print("Cookie 文件不存在！")
            Config.login_status = True
            return


    def init_user():
        """初始化 UserList"""
        for k, _ in Config.CookiesDict.items():
            Config.UserList.append(k)


    def init():
        # 读取本地 cookie
        RedInit.init_cookie()
        # 初始化所有用户
        RedInit.init_user()
        # 初始化浏览器
        RedInit.init_browser()
